;(function($){
	$.fn.xsxmFilter = function(opts){
		var el = this;
		var opt = {
			datasource : [],
			dataBack : function(){},
			closeBack : function(){},
		}
		var param = $.extend(opt,opts);
		var app = new App(el,param);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		
		this.initHmtl();
		this.initGrid();
		this.bindEvent();
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//返回
			el.find("#btnCancel").unbind("click").click(function(){
				
				el.empty();
				el.modal("hide");
				param.closeBack();
			})
			
			//确定
			el.find("#btnSure").unbind("click").click(function(){
				var ids = $("#modalGrid").jqGrid('getGridParam', 'selarrrow');
				var xhs = ""
				$.each(ids,function(key,value){
					var rowData=jQuery("#modalGrid").jqGrid("getRowData",value);
					xhs = xhs+","+rowData.xhs
				})
				var xhs_arr= []
				$.each(xhs.split(","),function(key,value){
					if(value!=""){
						xhs_arr.push(value)
					}
				})
				el.empty();
				el.modal("hide");
				param.dataBack(xhs_arr)
				
			});
			
			//全选
			el.find("#btnSelAll").unbind("click").click(function(){
				var ids = $("#modalGrid").getDataIDs();
				$.each(ids,function(key,value){
					$("#modalGrid").jqGrid("setSelection", value, false);
				})
			});
			
		},
		initHmtl : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			el.addClass("modal").attr("tabindex","-2").attr("data-backdrop","static")
    		var html =	'<div class="modal-dialog remark_order remark_dish" style="width:800px;">'+
				    		'<div id="pageOverlay" class="hide"></div>'+
							'<div id="loading" class="loading hide">'+
								'<i class="ace-icon fa fa-spinner fa-spin orange bigger-250"></i>'+
							'</div>'+
							'<div class="modal-content" style="width:800px;">'+     
					  			'<div class="modal-header no-padding">'+
									'<div class="table-header widget-header-large " style="text-align:center; font-size:20px">'+  
										'<span>'+$.i18n.propJsp("过滤")+'</span>'+       
									'</div>'+
								'</div>'+
								'<div class="modal-body">'+ 
									'<div class="widget-box">'+
										'<table id="modalGrid"></table>'+
//										'<div id="modalPage"></div>'+
									'</div>'+
								'</div>'+	
								'<div class="modal-footer">'+
									'<div class="clearfix">'+
										'<div class="pull-left">'+
											'<button id="btnSelAll" class="btn btn-info btn-lg btn-round">'+  
												'<i class="ace-icon fa fa-check"></i>'+$.i18n.propJsp("全选")+
											'</button>'+
										'</div>'+
										'<div class="pull-right">'+
											'<button id="btnSure" class="btn btn-info btn-lg btn-round">'+  
												'<i class="ace-icon fa fa-check"></i>'+$.i18n.propJsp("确定")+
											'</button>'+
											'<button id="btnCancel" class="btn btn-default btn-lg btn-round" data-dismiss="modal">'+  
												'<i class="ace-icon fa fa-undo"></i>'+$.i18n.propJsp("返回")+'</button>'+ 
											'</button>'+
										'</div>'+
									'</div>'+
								'</div>'+		
			  				'</div>'+
			  			'</div>';		
			el.html(html);
		},
		initGrid : function(){
			
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var grid_selector = el.find("#modalGrid");
			
			grid_selector.jqGrid({
				datatype : "local",
				width :766,
				height : 300,
				data : [],
				colNames : ["编号","名称","数量","金额","xhs"],
				colModel : [{
					name : "bh",//"名称",
					index : "bh",
					width: 150,
					key:true,
					hidden:true,
				},{
					name : "mc",//"名称",
					index : "mc",
					width: 150,
				},{
					name : "sl",//"数量",
					index : "sl",
					width: 100,
					align : "right",
				},{
					name : "amount",//"金额",
					index : "amount",
					width: 100,
					align : "right",
				},{
					name : "xhs",//"",
					index : "xhs",
					width: 100,
					hidden:true,
				}],    	
				scroll : true,
				sortname : "mc",
				sortorder : "asc",
				viewrecords : true,
				emptyrecords: $.i18n.propJsp("没有搜索到任何数据"),
				recordtext: $.i18n.propJsp("共计")+":{2}"+$.i18n.propJsp("条"),
				altRows : true, 
				scrollrows : true,
				autoScroll : true,
				shrinkToFit : true,
				multiselect : true,
		        ondblClickRow : function(rowid){  
		        },
		        onSelectRow:function(rowid){
		        },
				loadComplete : function(data){
					var table = this;
					setTimeout(function(){
//						updatePagerIcons(table);
//						enableTooltips(table);
					}, 0);
					el.find("#cb_modalGrid").hide();
//					var ids = grid_selector.jqGrid("getGridParam","selarrrow");
//					grid_selector.jqGrid("setSelection",ids[0]);
				}, 
			});
			t.initGridDs();
		},
		initGridDs :function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var list = param.datasource;
			var sorted = t.groupBy(list, function (item) {
			    return [item.bh];
			});
//			console.log(sorted);
			
			var new_array = []
			$.each(sorted,function(key,value){
				var json = {"mc":"","bh":"","sl":"0","amount":"0","xhs":""}
				$.each(value,function(kk,vv){
					json.mc = vv.mc
					json.bh = vv.bh
					json.sl = (Number(json.sl)+Number(vv.sl)).toFixed(2)
					json.amount = (Number(json.amount)+Number(vv.amount)).toFixed(2)
					json.xhs = json.xhs+","+vv.xh
				})
				new_array.push(json)
			})
//			console.log(new_array);
			
			var grid_selector = el.find("#modalGrid");
			grid_selector.jqGrid("clearGridData");
			$(new_array).each(function(k,v){
				grid_selector.jqGrid("addRowData",(k+1),v,"last");
			});
		},
		groupBy:function(array, f){
			var groups = {};
			  array.forEach(function (o) {
			    var group = JSON.stringify(f(o));
			    groups[group] = groups[group] || [];
			    groups[group].push(o);
			  });
			  return Object.keys(groups).map(function (group) {
			    return groups[group];
			  });
		}
	}
	
})(jQuery);